<?php
/**
 * Created by Alan.
 * User: Alan
 * Date: 2018\10\7 0007
 * Time: 18:06
 */

namespace app\bis\controller;


use app\common\model\BisAccount;
use app\common\validate\LoginValidate;
use think\Controller;

class Login extends Controller
{
    public function index()
    {
        if(request()->isPost()){
            //验证信息
            $validate = (new LoginValidate())->goCheck();
            if($validate !== true){
                return $this->error($validate);
            }

            $username = input('post.username','');
            $password = input('post.password','');
            //验证用户是否存在
            $bisAccount_model = new BisAccount();
            $user_info = $bisAccount_model->get(['username' => $username]);
            if(!$user_info || $user_info->status != 1){
                $this->error('该用户不存在，或者该用户未被审核通过');
            }

            //验证密码是否正确
            if($user_info->password != md5($password.$user_info->code)){
                $this->error('密码不正确');
            }

            $bisAccount_model->updateById(['last_login_time' => time()],$user_info->id);

            //保存用户信息
            session('bisAccount', $user_info, 'bis');

            return $this->success('登录成功', url('index/index'));
        }
        else {
            //获取session
            $account = session('bisAccount','','bis');
            if($account && $account->id){
                return $this->redirect(url('index/index'));
            }
            return $this->fetch();
        }
        return $this->fetch();
    }

    public function logout()
    {
        //清除sessiion
        session(null,'bis');
        //跳出
        return $this->redirect(url('login/index'));
    }
}